package com.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description :
 * @Author : Bruce Lee
 * @CreateTime : 2024/11/14
 */

@Configuration
public class RabbitmqConfig {

    /**
     * 普通交换机 X
     */
    public static final String EXCHANGE_X = "X";

    /**
     * 普通队列 QA
     */
    public static final String QUEUE_A = "QA";

    /**
     * 普通 routing key
     */
    public static final String ROUTING_KEY_XA = "XA";

    /**
     * 普通队列 QB
     */
    public static final String QUEUE_B = "QB";

    /**
     * 普通队列 QC
     */
    public static final String QUEUE_C = "QC";


    /**
     * 普通 routing key
     */
    public static final String ROUTING_KEY_XB = "XB";

    /**
     * 普通 routing key
     */
    public static final String ROUTING_KEY_XC = "XC";

    /**
     * 死信交换机 Y
     */
    public static final String DEAD_EXCHANGE_Y = "Y";

    /**
     * 死信队列 QD
     */
    public static final String DEAD_QUEUE_D = "QD";

    /**
     * 死信 routing key
     */
    public static final String DEAD_ROUTING_KEY_YD = "YD";

    /**
     * 普通交换机
     */
    public static final String EXCHANGE = "delayed.exchange";

    /**
     * routingkey
     */
    public static final String ROUTING_KEY = "delayed.routingkey";

    /**
     * 普通队列
     */
    public static final String QUEUE = "delayed.queue";

    /**
     * 声明交换机
     */
    @Bean
    public DirectExchange xExchange() {
        return new DirectExchange(EXCHANGE_X);
    }

    /**
     * 声明交换机
     */
    @Bean
    public DirectExchange yExchange() {
        return new DirectExchange(DEAD_EXCHANGE_Y);
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue aQueue() {
        return QueueBuilder.durable(QUEUE_A)
                // 声明当前队列绑定的死信交换机
                .deadLetterExchange(DEAD_EXCHANGE_Y)
                // 声明当前队列绑定的死信队列
                .deadLetterRoutingKey(DEAD_ROUTING_KEY_YD)
                // 设置 TTL 时间
                .ttl(10 * 1000)
                .build();
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue bQueue() {
        return QueueBuilder.durable(QUEUE_B)
                // 声明当前队列绑定的死信交换机
                .deadLetterExchange(DEAD_EXCHANGE_Y)
                // 声明当前队列绑定的死信队列
                .deadLetterRoutingKey(DEAD_ROUTING_KEY_YD)
                // 设置 TTL 时间
                .ttl(40 * 1000)
                .build();
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue cQueue() {
        return QueueBuilder.durable(QUEUE_C)
                .deadLetterExchange(DEAD_EXCHANGE_Y)
                .deadLetterRoutingKey(DEAD_ROUTING_KEY_YD)
                .build();
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue dQueue() {
        return QueueBuilder.durable(DEAD_QUEUE_D).build();
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding xaBinding() {
        return BindingBuilder.bind(aQueue()).to(xExchange()).with(ROUTING_KEY_XA);
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding xbBinding() {
        return BindingBuilder.bind(bQueue()).to(xExchange()).with(ROUTING_KEY_XB);
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding xcBinding() {
        return BindingBuilder.bind(cQueue()).to(xExchange()).with(ROUTING_KEY_XC);
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding ydBinding() {
        return BindingBuilder.bind(dQueue()).to(yExchange()).with(DEAD_ROUTING_KEY_YD);
    }

    @Bean
    public CustomExchange exchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue queue() {
        return QueueBuilder.durable(QUEUE).build();
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(ROUTING_KEY).noargs();
    }

}
